package com.shop.operator.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shop.domain.PageResult;
import com.shop.model.Content;
import com.shop.model.ContentCategory;
import com.shop.operator.service.ContentService;

/**
 * 广告管理界面
 * @author Administrator
 *
 */
@Controller
public class ContentAction {

	@Resource
	private ContentService contentService;
	/**
	 * 返回广告管理界面
	 * http://localhost:8080/Shopping/returncontent.action
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/returncontent")
	public String returncontent() throws Exception{
		
		return "forward:/WEB-INF/operator/admin/content.jsp";
	}
	
	/**
	 * 查找所有的广告类
	 * http://localhost:8080/shopping/findContent.action?page=1&size=5
	 * @param page
	 * @param size
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/findContent")
	public PageResult findContent(int page,int size) throws Exception{
		
		return this.contentService.findContent(page, size);
	}
	
	/**
	 * 根据id查找
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/findOneContentById")
	public Content findOneContentById(Long id) throws Exception{
		
		System.out.println("根据id查找"+id);
		return this.contentService.findOneContent(id);
	}
	/**
	 * 更新商品
	 * @param contentCategory
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/updateContent")
	public Map<String,String> update(@RequestBody Content content) throws Exception{
     
		boolean	 bool = this.contentService.update(content);
				System.out.println("更新广告");
		Map<String,String> map = new HashMap<String, String>();
		
		if(bool){
			map.put("success", "true");
		}else
		{
			map.put("message", "更新失败,该广告名已经存在");
		}
		return map;
	}
	
	/**
	 * 添加商品
	 * @param contentCategory
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/addContent")
	public Map<String,String> add(@RequestBody Content content) throws Exception{
		System.out.println("content="+content);
		boolean bool = this.contentService.add(content);
		System.out.println("添加广告");
		Map<String,String> map = new HashMap<String, String>();
		
		if(bool){
			map.put("success", "true");
		}else
		{
			map.put("message", "更新失败,该广告名已经存在");
		}
		return map;
	}
    /**
     * 删除商品
     */
	
	@ResponseBody
	@RequestMapping("/deleteContent")
	public Map<String,String> delete(Long[] ids) throws Exception{
		boolean bool = this.contentService.delete(ids);
		Map<String,String> map = new HashMap<String, String>();
		
		if(bool){
			map.put("success", "true");
		}else
		{
			map.put("message", "删除失败");
		}
		return map;
	}
	
	
}
